package cn.bonoon.kernel.web.resources;

import org.apache.velocity.runtime.resource.loader.StringResourceLoader;

/**
 * <pre>
 * 用于存储自动生成的界面；自动生成后是字符串的html源码，需要在系统内缓存起来，下次调用的时候不需要再生成一次；
 * 
 * 这里可以叫“源码资源”，这些自动生成的源码资源所必须包含的属性包括：
 * 	1. 路径：模板引擎可以通过该路径定位到这一个源码资源包
 * 	2. html源码
 * 	3. 名称： 没有后缀的路径
 * 	4. 
 * </pre>
 * @author jackson
 *
 */
public abstract class AbstractResourceRepository{

	protected final static String VM_EXT = ".vm";
	protected final String path;
	protected final String name;
	
	/**
	 * 直接放在&lt;script&gt;标签里的js代码
	 */
	protected final StringBuilder script;
	/**
	 * <pre>
	 * 将被放在
	 * &lt;script&gt;
	 *     $(function(){
	 *         //code
	 *     });
	 * &lt;/script&gt;
	 * 里的js代码
	 * </pre>
	 */
	protected final StringBuilder block;
	
	protected final StringBuilder includes;
	
	protected final String id;
	
	public AbstractResourceRepository(String name, String id,
			StringBuilder includes, 
			StringBuilder script, 
			StringBuilder block) {
		this.includes = includes;
		this.script = script;
		this.block = block;
		this.id = id;
		this.name = name + '.' + id;
		this.path = this.name + VM_EXT;
	}
	
	public String getName() {
		return name;
	}
	
	public StringBuilder getBlock() {
		return block;
	}
	
	public StringBuilder getIncludes() {
		return includes;
	}
	
	public StringBuilder getScript() {
		return script;
	}
	
	public void putStringResource(StringBuilder html){
		StringResourceLoader.getRepository().putStringResource(path, html.toString());
	}
}
